//
//  LoginServer.m
//  HiQ
//
//  Created by arvintan on 12-7-25.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "LoginServer.h"
#import "Buddy.h"
#import "BuddyListSource.h"
#import "EncryptorWithKey.h"
#import "MainWindowController.h"

NSString *LOG_KEY = @"password";

static LoginServer *sharedLoginServer;

@implementation LoginServer

@synthesize delegate;

- (id)initWithHost:(NSString*)host andPort:(int)port
{
    connection = [[Connection alloc] initWithHostAddress:host andPort:port];
    id pdc = [[PacketDispathCenter alloc]init];
    //这里为了去除warning，以后再改
    (void)pdc;
    sharedLoginServer = self;
    return self;
}

+ (id) sharedLoginServer
{
    return sharedLoginServer;
}

- (void) sendMsg:(NSString *)msg
{
    [connection sendMsg:msg cmd:CMD_FUN_LOG subcmd:CMD_LOG_LOGIN];
}

-(void) sendLoginMsg:(NSString *)name Password:(NSString*) password
{    
    NSUInteger userID = [name integerValue];
    NSMutableData *data = [[NSMutableData alloc] initWithBytes:&userID length:sizeof(NSUInteger)];
    NSData *encryptPwd = [EncryptorWithKey encryptWithKey:[password dataUsingEncoding:NSUTF8StringEncoding] forKey:LOG_KEY];
    NSUInteger pwdLength = [encryptPwd length];
    [data appendBytes:&pwdLength length:sizeof(NSUInteger)];
    [data appendData:encryptPwd];
    
    [connection sendData:data cmd:CMD_FUN_LOG subcmd:CMD_LOG_LOGIN];
    loginTimeOut = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(onLoginTimeOut:) userInfo:nil repeats:NO];
}

-(void) onLoginTimeOut:(NSTimer *)timer
{   
    [loginTimeOut invalidate];
    dispatch_async(dispatch_get_main_queue(), ^{
        [delegate loginTimeOut];
    });
}


-(void) onReceiveLoginPacket:(NSData *)data
{
    [loginTimeOut invalidate];
    void *bytes = (void*)[data bytes];
    NSUInteger subFun =*((NSUInteger *)(bytes + sizeof(NSUInteger)));
    
    
    //是否允许登陆
    switch (subFun) {
            //允许登陆
        case CMD_LOG_RET_YES:
        {
            NSUInteger userID = *((NSUInteger *)(bytes + 2*sizeof(NSUInteger)));
            NSUInteger nameLen = *((NSUInteger *)(bytes + 3*sizeof(NSUInteger)));
            NSUInteger imageLen = *((NSUInteger *)(bytes + 4*sizeof(NSUInteger))); 
            NSString *userName = [[NSString alloc] initWithBytes:(bytes + 5*sizeof(NSUInteger)) length:nameLen encoding:NSUTF8StringEncoding];
            NSString *headImage = [[NSString alloc] initWithBytes:(bytes + 5*sizeof(NSUInteger) + nameLen) length:imageLen encoding:NSUTF8StringEncoding];
            
            Buddy *userInfo = [[Buddy alloc] init];
            userInfo.buddyQQ = userID;
            userInfo.buddyName = userName;
            userInfo.photo = headImage;
            userInfo.status = BUDDY_STATUS_ONLINE;
            dispatch_async(dispatch_get_main_queue(), ^{
                [delegate loginComplete:YES UserInfo:userInfo];
            });            
        }
            break;
            //不允许登陆
        case CMD_LOG_RET_NO:
        {
            dispatch_async(dispatch_get_main_queue(), ^{
                [delegate loginComplete:NO UserInfo:nil];
            });
        }
            break;
        case CMD_LOG_REPEAT_LOGIN:
        {
            dispatch_async(dispatch_get_main_queue(), ^{
                [delegate loginRepeat:YES];
            });
        }
            break;
        case CMD_LOG_CHANGE_LOGIN:
            dispatch_async(dispatch_get_main_queue(), ^{
                [[MainWindowController sharedMainWindowController] onChangeLogin];
            });
            break;
            //未知情况
        default:
            NSLog(@"Unknow Login request return");
            break;
    }

}

@end
